/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.minimalismstyle.fresh.sys.controller

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.security.util.SecurityContextUtils
import com.gitee.minimalismstyle.fresh.common.core.util.TreeUtils
import com.gitee.minimalismstyle.fresh.sys.api.service.SysResourceService
import com.gitee.minimalismstyle.fresh.sys.api.service.SysUserService
import com.gitee.minimalismstyle.fresh.sys.vo.admin.App
import com.gitee.minimalismstyle.fresh.sys.vo.admin.Menu
import com.gitee.minimalismstyle.fresh.sys.vo.admin.SysInfo
import io.swagger.annotations.Api
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import kotlin.streams.toList

/**
 *
 * @author maoxiaodong
 */
@Api(value = "admin", tags = ["PC端全局管理"])
@RestController
@RequestMapping("/admin")
class AdminController(private val sysUserService: SysUserService,
                      private val sysResourceService: SysResourceService) {

    /**
     * 获取系统基本信息
     * @return
     */
    @GetMapping("getSysInfo")
    fun getSysInfo(): R<SysInfo> {
        val user = SecurityContextUtils.user()
        val sysResourceList = sysResourceService.findByType("MAIN_MENU")

        val sysInfo = SysInfo()
        val app = App()
        app.name = "尝鲜"
        app.description = "尝鲜"
        sysInfo.app = app

        sysInfo.user = sysUserService.findUserRolesByUsername(user.username).get().toUser()
        val rootMenu = Menu("0")
        rootMenu.text = "主菜单"
        rootMenu.children = TreeUtils.build(sysResourceList.get().map {
            it.toMenu()
        }) as MutableList<Menu>
        sysInfo.menus = arrayListOf(rootMenu)

        return R.success(sysInfo)
    }

}